
#include <iostream>
#include "Record.h"
#include <stdlib.h>
using namespace std;

extern "C" {
	int yyparse(void);   // defined in y.tab.c
}

extern struct AndList *final;

int main () {

	// try to parse the CNF
	/*cout << "Enter in your CNF: ";
  	if (yyparse() != 0) {
		cout << "Can't parse your CNF.\n";
		exit (1);
	}*/

	// suck up the schema from the file
	Schema lineitem ("catalog", "lineitem");

	// grow the CNF expression from the parse tree 
	/*CNF myComparison;
	Record literal;
	myComparison.GrowFromParseTree (final, &lineitem, literal);
	
	print out the comparison to the screen
	myComparison.Print ();*/

	// now open up the text file and start procesing it
       FILE *tableFile = fopen ("/home/abhijeet/ufl/dbi/db/lineitem.tbl", "r");

       Record temp;
       Schema mySchema ("catalog", "lineitem");

	//char *bits = literal.GetBits ();
	//cout << " numbytes in rec " << ((int *) bits)[0] << endl;
	//literal.Print (&supplier);

        // read in all of the records from the text file and see if they match
	// the CNF expression that was typed in
	/*int counter = 0;
	ComparisonEngine comp;
        while (temp.SuckNextRecord (&mySchema, tableFile) == 1) {
		counter++;
		if (counter % 10000 == 0) {
			cerr << counter << "\n";
		}

		if (comp.Compare (&temp, &literal, &myComparison))
                	temp.Print (&mySchema);

        }*/
        Page page;
        off_t pageCount=0;
        
        //Open a new file to save the records
        File file;
        
        
        int noOfrecords=0;
        //read from db and write to binary file;
        /*file.Open(0,"./lineitemFile.db");
        while (temp.SuckNextRecord(&mySchema, tableFile))
        {
			//temp.Print(&mySchema);
			noOfrecords+=1;
			if ( page.Append(&temp) )
			{
				cout << "record added to the page: " << noOfrecords << endl;
			}else{
				cout << "adding the page to file" << endl;
				file.AddPage(&page,pageCount);
				pageCount++;
				page.EmptyItOut();
				sleep(3);
				noOfrecords=0;
			}
		}
		file.Close();
        */
        //read form binary file and write to console
        file.Open(1,"./lineitemFile.db");
        off_t totalPages;
        totalPages = file.GetLength();
       
        cout << "total no of pages: " << totalPages << endl;
        sleep ( 10 );
        
        /*for ( int pagesLoop=0;pagesLoop<totalPages; pagesLoop++)
        {
			file.GetPage(&page,pagesLoop);
			cout << "Printing Page : " << pagesLoop+1 << endl;
			sleep ( 1 );
			while ( page.GetFirst ( &temp) ) {
				temp.Print(&mySchema);
			}
			
		}*/
        

}


